package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;

import Semantics.Semantics;
import Semantics.SemanticsException;
import Semantics.SemanticsOverload;
import Semantics.VarSymbol;
import SyntaxTree.ASTprogram;
import SyntaxTree.ParseException;
import SyntaxTree.XYZ2parser;


public class SeMain {
	public static void main(String args[])throws FileNotFoundException {
		    File file = new File("javatest.txt");
	
		    InputStream fis = new FileInputStream(file);
		    XYZ2parser parser = new XYZ2parser(fis);   

		    try {
		      ASTprogram parsertree = parser.program();
		      /*Output the exception */
		      for(int i = 0;i<parser.getExceptionList().size();i++){
		    	  System.out.println(parser.getExceptionList().get(i));
		      }
		      
		      Semantics semantics = new Semantics();
		      
		      semantics.TypeCheckTest(parsertree);
//		      parsertree.dump("");	      
		      ArrayList<SemanticsException> exceptions = semantics.getExceptions();
		      ArrayList<SemanticsOverload> overloads = semantics.getOverloads(); 
		      ArrayList<String> overload_message = new ArrayList<String>();
		      ArrayList<VarSymbol> vartable = semantics.getVartable();
		      System.out.println("Variable Table:");
		      for(int m = 0; m < vartable.size(); m++){
		    	  System.out.println(vartable.get(m).getId());  //get variable name
		    	  System.out.println(vartable.get(m).getType()); //get variable type
		    	  //System.out.println(vartable.get(m).getLine()); // get variable line
		      }
		      System.out.println("Exceptions:");
		      for(int i = 0; i < exceptions.size(); ++i){
		    	  System.out.println(exceptions.get(i).getMessage()); 
		    	  /*
		    	  System.out.println(exceptions.get(i).etype);
		    	  String[] info = exceptions.get(i).einfo;
		    	  int[] lines = exceptions.get(i).eline;
		    	  for(int j = 0; j < info.length; ++j){
		    		  System.out.println(lines[j] + " " + info[j]);
		    		
		    	  }
		    	  */
		      }
		      
		      System.out.println("****************************");
		      System.out.println("Overloads:");
		      for(int i = 0; i < overloads.size(); ++i){
		    	  overload_message.add(overloads.get(i).getMessage());
		      }
		      for(int i = 0; i< overload_message.size(); i++){
		    	  for(int j=i+1;j<overload_message.size();j++){
		    		  if(overload_message.get(j).equals(overload_message.get(i)))
		    			  overload_message.remove(j);
		    	  }
		      }
		      for(int i = 0; i < overload_message.size(); ++i){
		    	  System.out.println(overload_message.get(i));
		    	  /*
		    	  System.out.println(overloads.get(i).type);
		    	  MethodSymbol[] methods = overloads.get(i).methods;
		    	  for(int j = 0; j < methods.length; ++j){
		    		  System.out.println(methods[j].getLine() + " " 
		    				  	+ methods[j].getId() + " "
		    				  	+ methods[j].getParams().length);
		    	  }
		    	  */
		      }
		      
		      
		      
	//	      System.out.println("Java program parsed successfully.");
		    } catch (ParseException e) {
		      System.out.println(e.getMessage()); 
		      System.out.println("Encountered errors during parse.");
		    }
		}
	
}